package cassys.domain.classManagement;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author User
 */
public class ClassGroup  implements Serializable {

    private long classGroupId;
    private String title;
    private Module module;
    private Set<ClassRegistration> classMembers;

    public ClassGroup() {
        this.classMembers = new HashSet<ClassRegistration>();
    }

    public ClassGroup(Module module, String title) {
        this();
        this.title = title;
        this.module = module;
    }

    public void addStudent(Student student) {
        ClassRegistration registration = new ClassRegistration(ClassRegistrationStatus.active, student, this);
        this.getClassMembers().add(registration);
        student.signUpClassGroup(registration);
    }

    public int getClassSize() {
        return this.getClassMembers().size();
    }

    public void withdrawStudent(Student student) throws StudentNotFoundException {
        ClassRegistration registration = getRegistration(student);
        registration.setStatus(ClassRegistrationStatus.withdrawn);
        student.withdraw(this);
    }

    public ClassRegistration getRegistration(Student student) throws StudentNotFoundException {
        for (ClassRegistration participent : getClassMembers()) {
            if (participent.getStudent().equals(student)) {
                return participent;
            }
        }
        throw new StudentNotFoundException(this);
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    public Set<ClassRegistration> getClassMembers() {
        return classMembers;
    }

    public void setClassMembers(Set<ClassRegistration> classMembers) {
        this.classMembers = classMembers;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ClassGroup other = (ClassGroup) obj;

        return this.title.equals(other.title);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.title != null ? this.title.hashCode() : 0);
        return hash;
    }

    /**
     * @return the classGroupId
     */
    public long getClassGroupId() {
        return classGroupId;
    }

    /**
     * @return the module
     */
    public Module getModule() {
        return module;
    }

   
}
